package cn.stylefeng.guns.modular.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.storage.entity.StoragePut;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutParam;
import cn.stylefeng.guns.modular.storage.service.StoragePutService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 入库单表控制器
 */
@Controller
@RequestMapping("/storagePut")
public class StoragePutController extends BaseController {

    private String PREFIX = "/storage/storagePut";

    @Autowired
    private StoragePutService storagePutService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/storagePut.html";
    }

    /**
     * 入库单查询页面
     */
    @RequestMapping("toQuery")
    public String toQuery(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/query.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/storagePut_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/storagePut_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StoragePutParam storagePutParam) {
        this.storagePutService.add(storagePutParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StoragePutParam storagePutParam) {
        this.storagePutService.update(storagePutParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StoragePutParam storagePutParam) {
        this.storagePutService.delete(storagePutParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StoragePutParam storagePutParam) {
        StoragePut detail = this.storagePutService.getById(storagePutParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StoragePutParam storagePutParam,String condition ) {
    	User user = userService.getcurrentUser();
    	storagePutParam.setDeliverId(user.getDeliverId());
    	storagePutParam.setBillstate("webstorage");
    	Long deptId = user.getDeptId();
        LayuiPageInfo layuiPageInfo = this.storagePutService.findPageBySpec(storagePutParam,condition,deptId,user.getOperateId());
        return layuiPageInfo;
    }

    /**
     * 查询列表
     * 入库单查询
     */
    @ResponseBody
    @RequestMapping("/querylist")
    public LayuiPageInfo querylist(StoragePutParam storagePutParam,String condition,Long operateId,String startSingleTime,String endSinglePutTime) {
    	User user = userService.getcurrentUser();
    	storagePutParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
    	String singlePutTime = "";
    	if (ToolUtil.isNotEmpty(startSingleTime) && ToolUtil.isNotEmpty(endSinglePutTime)) {
    		singlePutTime = "'"+startSingleTime+"' and '"+endSinglePutTime+"'";
		} else if (startSingleTime == null && endSinglePutTime == null){
			startSingleTime = DateUtil.getDate();
			endSinglePutTime = DateUtil.getDate();
			singlePutTime = "'"+startSingleTime+"' and '"+endSinglePutTime+"'";
		}
        LayuiPageInfo layuiPageInfo = this.storagePutService.querylistPageBySpec(storagePutParam,condition,deptId,user.getOperateId(),singlePutTime);
        return layuiPageInfo;
    }

}


